package com.qijian.maindata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.maindata.domain.LineProcess;
import com.qijian.maindata.domain.ProcessFileViewHistory;
import com.qijian.maindata.domain.vo.ProcessFileViewGroupVo;
import com.qijian.maindata.domain.vo.ProcessFileViewStorageVo;
import com.qijian.maindata.domain.vo.ProcessFilesVo;
import com.qijian.maindata.mapper.ProcessFileViewHistoryMapper;
import com.qijian.maindata.service.IProcessFileViewHistoryService;
import com.qijian.tool.domain.Storage;
import com.qijian.tool.service.IStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ProcessFileViewHistoryServiceImpl extends ServiceImpl<ProcessFileViewHistoryMapper, ProcessFileViewHistory> implements IProcessFileViewHistoryService {

    private final IStorageService storageService;

    @Override
    public boolean batchBindFiles(ProcessFilesVo filesVo) {
        if(CollUtil.isEmpty(filesVo.getProcessIds())){
            throw new ServiceException("E01:数据异常,工序为空");
        }
        if(CollUtil.isEmpty(filesVo.getFiles())){
            throw new ServiceException("E01:数据异常,文件列表为空");
        }
        if(Objects.equals(filesVo.getType(),0)){
            //覆盖,清空processId对应的所有文件,用新的文件进行存储
            storageService.remove(new LambdaQueryWrapper<Storage>()
                    .in(Storage::getClassId,filesVo.getProcessIds()).eq(Storage::getClassTable, LineProcess.class.getSimpleName()));
        }
        //type!=0,追加的形式新增文件
        //每个processId保存同样一份文件
        List<Storage> storages = new ArrayList<>();
        for (Long processId : filesVo.getProcessIds()) {
            for (Storage storage : filesVo.getFiles()) {
                Storage targetStorage = new Storage();
                BeanUtil.copyProperties(storage,targetStorage);
                targetStorage.setClassId(processId);
                targetStorage.setClassTable(LineProcess.class.getSimpleName());
                targetStorage.setStorageId(null);
                storages.add(targetStorage);
            }
        }
        //批量保存
        if(CollUtil.isNotEmpty(storages)){
            storageService.saveBatch(storages);
        }
        return true;
    }

    @Override
    public List<ProcessFileViewStorageVo> getProcessBindFiles(Long processId, Long wuId) {
        if(Objects.isNull(processId)){
            return Collections.emptyList();
        }
        List<Storage> list = storageService.list(new LambdaQueryWrapper<Storage>()
                .eq(Storage::getClassTable, LineProcess.class.getSimpleName())
                .eq(Storage::getClassId, processId));
        if(CollUtil.isNotEmpty(list)){
            //补充是否校验的标识
            List<ProcessFileViewStorageVo> resultList = BeanUtil.copyToList(list, ProcessFileViewStorageVo.class);
            if(CollUtil.isNotEmpty(resultList)){
                resultList.forEach(e->e.setChecked(false));
            }
            if(Objects.nonNull(wuId)){
                List<ProcessFileViewGroupVo> storageGroupList = baseMapper.findStorageGroupList(processId, wuId, SecurityUtils.getUserId());
                if(CollUtil.isNotEmpty(storageGroupList)){
                    Map<Long, Long> collectMap = storageGroupList.stream().collect(Collectors.toMap(ProcessFileViewGroupVo::getStorageId, ProcessFileViewGroupVo::getCnt));
                    resultList.forEach(r->{
                        r.setChecked(!Objects.isNull(collectMap.get(r.getStorageId())));
                    });
                }
            }
            return resultList;
        }
        return Collections.emptyList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveView(ProcessFileViewHistory viewHistory) {
        viewHistory.setUserId(SecurityUtils.getUserId());
        viewHistory.setCreateBy(SecurityUtils.getNickName());
        viewHistory.setUpdateBy(SecurityUtils.getNickName());
        baseMapper.saveView(viewHistory);
        return true;
    }

    @Override
    public List<Storage> processBindFilesById(Long processId) {
        List<Storage> list = storageService.list(new LambdaQueryWrapper<Storage>()
                .eq(Storage::getClassTable, LineProcess.class.getSimpleName())
                .eq(Storage::getClassId, processId));
        return list;
    }

}
